import 'package:dio/dio.dart';
import 'package:love_app/common/su.dart';
import 'package:love_app/common/su_dio.dart';
import 'package:love_app/config/path_url.dart';
import 'package:love_app/config/setting.dart';
import 'package:love_app/enum/responst_status.dart';
import 'package:love_app/model/from_token_model.dart';
import 'package:love_app/model/qi_niu_upload_model.dart';
import 'package:love_app/model/response_model.dart';
import 'package:love_app/model/upload_token_model.dart';

class AddRecordApi {
  String apiPath; // 发表的api地址
  AddRecordApi({this.apiPath});

  // 表单形式上传到七牛云
  Future<List<String>> _uploadQiNiu(
      String token, List<String> listFilePath) async {
    List<String> uploadToken = await _getUploadToken(token, listFilePath);
    return uploadToken;
  }

  // 发表动态
  Future<bool> create(
    String token, List<String> listFilePath, String content, String formToken) async {
    // 获取成功上传的image_key
    List<String> allKey = await _uploadQiNiu(token, listFilePath);
    bool success = await _addDynamic(allKey, formToken, content, token);
    return success;
  }

  // 上传单个文件到七牛云
  Future<String> uploadOne(String token, String filePath)async{
    List<String> listFilePath = <String>[filePath];
    List<String> keys = await _uploadQiNiu(token, listFilePath);
    return keys.first;
  }

  // 返回发表内容api地址
  static String getPostApi(String type){
    if(type == 'love_log'){
      return PathUrl.addLoveDynamic;
    }return PathUrl.addDynamic;
  }

  // 只上传文字
  Future<bool> createOnlyText(String token, String content, String formToken) async {
    Map headers = {
      'X-Auth-Token': token,
    };
    Map formData = {
      'content': content,
      'form_token': formToken,
    };
    ResponseModel responseModel = await SuDio()
        .formDataRequest(apiPath, formDataMap: formData, headersMap: headers);
    if (responseModel.status == ResponseStatusType.success) {
      return true;
    }
    return false;
  }


  // 获取七牛云返回的所有key
  Future<List<String>> _getUploadToken(
      String token, List<String> listFilePath) async {
    Map headers = {
      'X-Auth-Token': token,
    };
    String uploadPath = PathUrl.getUploadToken + '/${listFilePath.length}';
    ResponseModel responseModel =
        await SuDio().getRequest(uploadPath, headersMap: headers);
    UploadTokenModel uploadTokenModel =
        UploadTokenModel.fromJson(responseModel.toJsonModel);
    List uploadTokenList = uploadTokenModel.data;
    int index = 0;
    List<String> allKey = []; // 定义一个空数组， 用于存储所有上传图片成功七牛云返回的key
    for (String filePath in listFilePath) {
      String key = await _uploadImage(uploadTokenList[index], filePath);
      allKey.add(key);
      index++;
    }
    return allKey;
  }

  // 上传图片到七牛，返回成功七牛云返回的key
  Future<String> _uploadImage(String uploadToken, String filePath) async {
    MultipartFile file =
        await MultipartFile.fromFile(filePath, filename: filePath);
    Map formData = {
      'action': Setting.QiNiuUploadPath,
      'token': uploadToken,
      'file': file,
    };
    ResponseModel responseModel = await SuDio()
        .formDataRequest(Setting.QiNiuUploadPath, formDataMap: formData);
    QiNiuUploadModel qiNiuUploadModel =
        QiNiuUploadModel.fromJson(responseModel.toJsonModel);
    return qiNiuUploadModel.data.key;
  }

  // 生成表单上传内容的key
  String _getMapKey(String imageKey, int index) {
    String mapKey = "attachment" + "[$index]";
    return mapKey;
  }

  // 获取重复表单的token
  Future<String> getFormToken(String token) async {
    Map headers = {
      'X-Auth-Token': token,
    };
    String path = PathUrl.getFormToekn;
    ResponseModel responseModel =
        await SuDio().getRequest(path, headersMap: headers);
    FromTokenModel fromTokenModel =
        FromTokenModel.fromJson(responseModel.toJsonModel);
    return fromTokenModel.data.formToken;
  }

  // 发表社区文章
  Future<bool> _addDynamic(List<String> allKey, String formToken,
    String conetent, String token) async {
    Map formData = _getFormData(allKey, formToken, conetent);
    Map headers = {
      'X-Auth-Token': token,
    };
    ResponseModel responseModel = await SuDio()
        .formDataRequest(apiPath, formDataMap: formData, headersMap: headers);
    Su.suPrints(responseModel.response);
    if (responseModel.status == ResponseStatusType.success) {
      return true;
    }
    return false;
  }

  // 获取表单上传map
  Map _getFormData(List<String> allKey, String formToken, String content) {
    Map formData = {
      'form_token': formToken,
      'content': content,
    };
    int index = 0;
    for (String key in allKey) {
      String mapKey = _getMapKey(key, index);
      formData[mapKey] = key;
      index++;
    }
    return formData;
  }
}
